package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.SystemMessage;
import com.aoxon.cargo.cache.SupCache;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.SystemMessageDetailService;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.ToastUtil;
import com.google.gson.Gson;

/**
 * 系统消息详细显示界面
 * 
 * @author ZZJ
 * 
 */
public class SupSystemMessageDetailActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private ImageView ivReturnButton;
	private ImageView ivDetailPicture;

	private ProgressBar progressBar;

	private TextView tvTitle;
	private TextView tvDetailTitle;
	private TextView tvDetailTime;
	private TextView tvDetailtext;

	private GsonBean gsonBean;
	private MyProgressDialog myProgressDialog;
	private FileLoader imageLoader = new FileLoader(800, 800, false);

	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	private SystemMessageDetailService systemMessageDetailService = (SystemMessageDetailService) MyServiceFactory
			.getInstance(MyServiceFactory.SYSTEM_MESSAGE_DETAIL);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setup();
	}



	private void setup() {
		setContentView(R.layout.sup_system_message_detail);

		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivDetailPicture = (ImageView) findViewById(R.id.ivSystemMessagePicture);

		progressBar = (ProgressBar) findViewById(R.id.pbSystemMessageLoading);

		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvDetailTitle = (TextView) findViewById(R.id.tvSystemMessageDetailTitle);
		tvDetailTime = (TextView) findViewById(R.id.tvSystemMessageDetailTime);
		tvDetailtext = (TextView) findViewById(R.id.tvSystemMessageDetailText);

		tvTitle.setText("系统消息");
		tvDetailTitle.setText(SupCache.systemMessage.getTitle());
		myProgressDialog = new MyProgressDialog(this, true);

		ivReturnButton.setOnClickListener(myClickListener);

		normalDataLoader.setOnloadDataListener(this);

		normalDataLoader.load();

	}

	/**
	 * 
	 */
	private void beginShow() {

		try {
			if (CheckStateUtil.check(getBaseContext(), gsonBean, "获取系统信息")) {
				SystemMessage systemMessage = new Gson().fromJson(
						gsonBean.getStrJson(), SystemMessage.class);
				tvDetailTitle.setText(systemMessage.getTitle());
				tvDetailTime.setText(systemMessage.getPostTime());
				tvDetailtext.setText(systemMessage.getContent());
				String path = systemMessage.getPicture().getPictureUrl()
						+ "l.png";
				imageLoader.displayImage(path, ivDetailPicture, progressBar,
						false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			progressBar.setVisibility(View.GONE);
		}

	}

	/**
	 * 系统按键的设置
	 * *****************************************************************************************************************************************/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(getBaseContext(),
					SupSystemMessageListActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	/**
	 * 设置各控件的点击监听
	 */
	OnClickListener myClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {

			case R.id.ivPublicTitleReturn:
				Intent intent = new Intent(getBaseContext(),
						SupSystemMessageListActivity.class);
				startActivity(intent);
				SupSystemMessageDetailActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "信息加载中..");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		gsonBean = systemMessageDetailService.execute(SupCache.systemMessage
				.getMessageId());
		msg.what = MyStateUtil.SUCCESS_RESULT;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		myProgressDialog.cancle();
		switch (state) {
		case MyStateUtil.SUCCESS_RESULT:
			beginShow();
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			break;
		}
	}

}
